package cn.jbooter.coms.utils;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import cn.jbooter.coms.exceptions.FrameworkException;

/**
 * string util
 * @author wl
 *
 */
public class StringUtil {
	
	/**
	 * 判断字符串是否为空
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str){
		return (str==null || str.trim().length()==0) ;
	}
	
	/**
	 * 把逗号隔开的字符串转成LIST
	 * @param orgStr String orgStr = "ID10002,ID10003,ID10004,ID10005,ID10006";
	 * @return
	 */
	public static List<String>  strSplit(String orgStr){
		try {
			if(StringUtil.isEmpty(orgStr)){
				return null;
			}
			String [] orgStrs = orgStr.split(",");
			return Arrays.asList(orgStrs);
		} catch (Exception e) {
			throw new FrameworkException("参数不合法");
		}
	}
	
	/**
	 * 生成UUID
	 */
	public static String getUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	/**
	 * 
	 * @param prefix 格式化出来的String的抬头
	 * @param no 在传入的这个no数字的基础上+1
	 * @return
	 */
	private static final String STR_FORMAT = "000000";
	public static String getFormtStr(String prefix,int no){
		no = no+1;
		DecimalFormat df = new DecimalFormat(STR_FORMAT);  
		return prefix+df.format(no);
	}
	/**
	 * List转逗号分隔字符串
	 */
	public static String listToString(List<String> list) {
	    StringBuilder sb = new StringBuilder();
	    if (list != null && list.size() > 0) {
	        for (int i = 0; i < list.size(); i++) {
	            if (i < list.size() - 1) {
	                sb.append(list.get(i) + ",");
	            } else {
	                sb.append(list.get(i));
	            }
	        }
	    }
	    return sb.toString();
	}
	
	/**
	 * 数组转逗号分隔字符串
	 */
	public static String converToString(String[] ig) {
	    String str = "";  
	    if (ig != null && ig.length > 0) {  
	        for (int i = 0; i < ig.length; i++) {  
	            str += ig[i] + ",";  
	        }  
	    }  
	    str = str.substring(0, str.length() - 1);  
	    return str;  
	}

	/**
	 * 指定分隔符连接字符串
	 * @param array
	 * @param separator
	 * @return
	 */
	public static String join(String[] array, String separator) {
		if (array == null) {
            return null;
        }
        if (separator == null) {
            separator = "";
        }
        // endIndex - startIndex > 0:   Len = NofStrings *(len(firstString) + len(separator))
        //           (Assuming that all Strings are roughly equally long)
        final int startIndex=0;
        final int endIndex=array.length;
        final int noOfItems = endIndex - startIndex;
        if (noOfItems <= 0) {
            return "";
        }
        final StringBuilder buf = new StringBuilder(noOfItems * 16);
        for (int i = startIndex; i < endIndex; i++) {
            if (i > startIndex) {
                buf.append(separator);
            }
            if (array[i] != null) {
                buf.append(array[i]);
            }
        }
        return buf.toString();
	}
	
	public static void main(String[] args) {
		String[] ss = {"xx","yy"};
		System.out.println(join(ss,"||"));
	}
}